package com.yuexunit.finance.system.action.v1.dto;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yuexunit.account.entity.AccountNameEntity;
import com.yuexunit.account.service.AccountService;
import com.yuexunit.finance.context.FinanceLetterBlockingQueue;
import com.yuexunit.finance.project.action.v1.FinanceApplicationActionApiImpl;
import com.yuexunit.finance.system.service.AccountRoleService;
import com.yuexunit.finance.system.service.RoleOperationService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.organization.entity.EmployeeEntity;
import com.yuexunit.organization.service.EmployeeService;
import com.yuexunit.permission.entity.AccountRoleEntity;
import com.yuexunit.permission.entity.RoleOperationEntity;
import com.yuexunit.util.StringUtil;


/**
 * @author wenwenlong
 */
public class SendLetterMessage {
	public static void sendLetterMessage(String operationCode, String message) {
		Logger logger = LoggerFactory.getLogger(SendLetterMessage.class);

		RoleOperationService roleOperationService = ComponentFactory.loadClass(RoleOperationService.class);
		AccountRoleService accountRoleService = ComponentFactory.loadClass(AccountRoleService.class);
		EmployeeService employeeService = ComponentFactory.loadClass(EmployeeService.class);
		AccountService accountService = ComponentFactory.loadClass(AccountService.class);
		
		List<RoleOperationEntity> list = roleOperationService.inquireRoleOperationList(operationCode);
		List<Long> accountIds = new ArrayList<>();
		if (list != null) {
			for (RoleOperationEntity roe : list) {
				List<AccountRoleEntity> accountRoleEntityList = accountRoleService.inquireAccountRoleList(roe.getRoleId());
				StringBuffer sb = new StringBuffer();
				for (AccountRoleEntity are : accountRoleEntityList) {
					logger.info("SendLetterMessage accountId1:"+are.getAccountId());
					EmployeeEntity ee = employeeService.inquireEmployeeByAccountId(null, are.getAccountId());
					if (ee == null) {
						continue;
					}
					accountIds.add(are.getAccountId());
					logger.info("SendLetterMessage accountId2:"+ee.getAccountId());
//					FinanceLetterBlockingQueue.putPlatformFinanceLetter(accountId, title, "finance.application.unaudit", message);
					sb.append(ee.getMobile()).append(",");
				}
				if(sb.length()>0) {
					sb.deleteCharAt(sb.length() - 1);
				}
				if (StringUtil.isNotBlank(sb.toString())) {
					for (String m : sb.toString().split(",")) {
						AccountNameEntity accountName = accountService.inquireAccountName(m);
						if (accountName != null) {
//							FinanceLetterBlockingQueue.putPlatformFinanceLetter(accountName.getAccountId(), "新的审批", "finance.application.unaudit", message);
						} else {
//							SendMsgUtil.sendMsg(m, message);
						}
					}
				}
			}
		}
		HashSet hs = new HashSet(accountIds);  
		accountIds.clear();  
		accountIds.addAll(hs);
		for (Long l : accountIds) {
			FinanceLetterBlockingQueue.putPlatformFinanceLetter(l, "新的审批", "finance.application.unaudit", message);
		}
		
//		FileWriter fw = null;
//        try {
//        	fw = new FileWriter("D:\\1.txt", true);
//        	fw.write(message + "\r\n");
//        	fw.close();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }finally{
//            if(fw != null){
//                try {
//                    fw.close();
//                } catch (IOException e) {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//            }
//        }
	}
}
